package com.primeton.fbsearch;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import net.paoding.analysis.analyzer.PaodingAnalyzer;
import net.paoding.analysis.knife.PaodingMaker;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.primeton.fbsearch.framework.ExtensionFileHandler;
import com.primeton.fbsearch.framework.FileHandler;
import com.primeton.fbsearch.framework.FileHandlerException;


public class IndexFiles
{
//  public static String SEARCH_DATA_HOME = "D:\\OpenSource\\search_data";

  public static String SEARCH_DATA_HOME = "D:\\CVSWork\\PSO";
  public static String INDEX_HOME = "D:\\OpenSource\\index";
	
  protected FileHandler fileHandler;
  
  public static Analyzer analyzer= null;
  
  public static PaodingAnalyzer panalyzer= null;  


  public IndexFiles()   {
	  analyzer = new org.apache.lucene.analysis.cjk.CJKAnalyzer();
	  //analyzer  = new ChineseAnalyzer();
	  //analyzer  = new  StandardAnalyzer();

	   panalyzer = new PaodingAnalyzer();
	  panalyzer.setKnife(PaodingMaker.make());
	  panalyzer.setMode(1);
  }

  public void index(IndexWriter writer, File file)
    throws FileHandlerException {

    if (file.canRead()) {
      if (file.isDirectory()) {
        String[] files = file.list();
        if (files != null) {
          for (int i = 0; i < files.length; i++) {
            index(writer, new File(file, files[i]));
          }
        }
      }
      else {
        System.out.println("********  Indexing file= " + file);
        try {
        	if(fileHandler == null) 
        		fileHandler = new ExtensionFileHandler();
        	
          Document doc = fileHandler.getDocument(file);       
          
          if (doc != null) {
   			Field FieldName = new Field("name", file.getName(), Field.Store.YES, Field.Index.TOKENIZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
    		doc.add(FieldName);           	  
        	  
  			Field FieldPath = new Field("path", file.getPath(),	Field.Store.YES, Field.Index.TOKENIZED,Field.TermVector.WITH_POSITIONS_OFFSETS); 			  			
			doc.add(FieldPath);
			
   			Field FieldNameSort = new Field("name_sort", file.getName(), Field.Store.YES, Field.Index.UN_TOKENIZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
    		doc.add(FieldNameSort);         			

   			Field FieldPathSort = new Field("path_sort", file.getName(), Field.Store.YES, Field.Index.UN_TOKENIZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
    		doc.add(FieldPathSort);       

		    doc.add(new Field("modified",
		            DateTools.timeToString(file.lastModified(), DateTools.Resolution.MINUTE),
		            Field.Store.YES, Field.Index.UN_TOKENIZED));
			
            writer.addDocument(doc);
          }
          else {
            System.err.println("Cannot handle "
              + file.getAbsolutePath() + "; skipping");
          }
        }
        catch (IOException e) {
          System.err.println("Cannot index "
            + file.getAbsolutePath() + "; skipping ("
            + e.getMessage() + ")");
        }
      }
    }
  }

  public static void main(String[] args) throws Exception {
	  
	  
	  
	  args = new String[2];
	 
	  args[0]=SEARCH_DATA_HOME;
	  args[1]=INDEX_HOME  ;
	  
    if (args.length < 2) {
      usage();
      System.exit(0);
    }
 
 

    Directory dir = FSDirectory.getDirectory(args[1],true);
   
    
     //Analyzer analyzer = new StandardAnalyzer();
    IndexFiles indexer = new IndexFiles();
    
    IndexWriter writer = new IndexWriter(dir, panalyzer, true);



    long start = new Date().getTime();
    indexer.index(writer, new File(args[0]));
    writer.optimize();
    writer.close();
    long end = new Date().getTime();

    System.out.println();
    IndexReader reader = IndexReader.open(dir);
    System.out.println("Documents indexed: " + reader.numDocs());
    System.out.println("Total time: " + (end - start) + " ms");
    reader.close();
  }

  private static void usage() {
    System.err.println("USAGE: java "
      + IndexFiles.class.getName()
      + "   /path/to/file/or/directory"
      + " /path/to/index");
  }
}
